package com.kh.pajx.sop.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.kh.pajx.sop.base.BaseDaoImpl;
import com.kh.pajx.sop.dao.ISysMenuAuthDao;
import com.kh.pajx.sop.domain.SysMenuAuth;

/**
 * 系统菜单数据持久化操作实现类
 * @author:	azzcsimp
 * @Createdate:	2014年7月20日 下午8:42:38
 */
@Repository
@SuppressWarnings("unchecked")
public class SysMenuAuthDaoImpl extends BaseDaoImpl<SysMenuAuth> implements
		ISysMenuAuthDao {
	
	/**
	 * 根据系统编码获取该系统下所有菜单信息列表
	 * @see com.kh.pajx.sop.dao.ISysMenuAuthDao#findAllMenuListBySys(java.lang.String)
	 */
	@Override
	public List<SysMenuAuth> findAllMenuListBySys(String systemCode) {
		return getSession()
				.createQuery(//
						"FROM SysMenuAuth menu WHERE menu.sysNameDc = ? ORDER BY menu.menuOrder")
				.setParameter(0, systemCode)
				.list();
	}
	
	/**
	 * 根据系统字典编码获取该系统下所有符合状态标识的菜单信息列表
	 * @see com.kh.pajx.sop.dao.ISysMenuAuthDao#findAllMenuListBySys(java.lang.String, int)
	 */
	@Override
	public List<SysMenuAuth> findAllMenuListBySys(String systemCode, int flag) {
		return getSession()
				.createQuery(//
						"FROM SysMenuAuth menu WHERE menu.sysNameDc = ? AND menu.menuStatusFlag = ? ORDER BY menu.menuOrder")
				.setParameter(0, systemCode)
				.setParameter(1, flag)
				.list();
	}
	
	/**
	 * 根据系统编码获取该系统下顶级菜单信息列表
	 * @see com.kh.pajx.sop.dao.ISysMenuAuthDao#findTopMenuListBySys(java.lang.String)
	 */
	@Override
	public List<SysMenuAuth> findTopMenuListBySys(String systemCode) {
		return getSession()
				.createQuery(//
						"FROM SysMenuAuth menu WHERE menu.parent.menuId IS NULL AND menu.sysNameDc = ? ORDER BY menu.menuOrder")
				.setParameter(0, systemCode)
				.list();
	}

	

	/**
	 * 查询一级菜单列表
	 * @see com.kh.pajx.sop.dao.ISysMenuAuthDao#findTopMenuAuthList()
	 */
	@Override
	public List<SysMenuAuth> findTopMenuAuthList() {
		return getSession()
				.createQuery(//
						"FROM SysMenuAuth ma WHERE ma.parent.menuId IS NULL ORDER BY ma.menuOrder")
				.list();
	}

	/**
	 * 查询二级菜单
	 * @see com.kh.pajx.sop.dao.ISysMenuAuthDao#findChilerenMenuAuthList(java.lang.String)
	 */
	@Override
	public List<SysMenuAuth> findChilerenMenuAuthList(String parentId) {
		return getSession()
				.createQuery(//
						"FROM SysMenuAuth ma WHERE ma.parent.menuId=? order by ma.menuOrder")//
				.setParameter(0, parentId)//
				.list();
	}

	/**
	 * 
	 * @see com.kh.pajx.sop.dao.ISysMenuAuthDao#counMenuAuthByPId(java.lang.String)
	 */
	public long counMenuAuthByPId(String pId) {
		return (Long) getSession()
				.createQuery(
						"SELECT COUNT(*) FROM SysMenuAuth ma WHERE ma.parent.menuId=?")
				.setParameter(0, pId).uniqueResult();
	}

	/*public List<SysMenuAuth> findTopMenuAuthListBySystemCode(String systemCode) {
		return getSession()
				.createQuery(//
						"FROM SysMenuAuth ma WHERE ma.parent.menuId IS NULL and ma.menuType=1 "
								+ "AND ma.sysNameDc=? "
								+ "ORDER BY ma.menuOrder")
				.setParameter(0, systemCode).list();
	}*/

	/*@Override
	public List<SysMenuAuth> findMenuAuthListBySystemCode(String systemCode) {
		return getSession()
				.createQuery(//
						"FROM SysMenuAuth ma WHERE " + "ma.sysNameDc=? "
								+ "ORDER BY ma.menuOrder")
				.setParameter(0, systemCode).list();
	}*/

	@Override
	public List<String> getAllMenuAuthUrls() {
		return getSession()
				.createQuery(//
						"SELECT DISTINCT menu.menuUrl FROM SysMenuAuth menu WHERE menu.menuUrl IS NOT NULL")
				.list();
	}

	@Override
	public List<String> getAllMenuAuthUrlsBySystemCode(String systemCode) {
		return getSession()
				.createQuery(//
						"SELECT DISTINCT menu.menuUrl FROM SysMenuAuth menu WHERE menu.menuUrl IS NOT NULL and menu.sysNameDc=?")
				.setParameter(0, systemCode).list();
	}

	@Override
	public List<SysMenuAuth> findSecondMenuAuthListBySystemCode(
			String systemCode) {
		return getSession()
				.createQuery(//
						"FROM SysMenuAuth ma WHERE ma.parent.menuId IS NOT NULL and ma.menuType=1 "
								+ "AND ma.sysNameDc=? "
								+ "ORDER BY ma.menuOrder")
				.setParameter(0, systemCode).list();
	}

}
